package com.owen.controller;

import com.owen.comment.R;
import com.owen.pojo.User;
import com.owen.server.UserServer;
import com.owen.utils.JxlUtil;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserServer userServer;

    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public R userList(){
        List<User> userList = userServer.UserList();
        return R.ok().put("data",userList);
    }

    @RequestMapping(value = "/user/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") Long id){
        User user = userServer.getById(id);
        return R.ok().put("user", user);
    }

    @RequestMapping(value = "/user/save",method = RequestMethod.POST)
    public R add(@RequestBody User user){
        userServer.save(user);
        return R.ok();
    }

    @RequestMapping(value = "/user/update",method = RequestMethod.POST)
    public R update(@RequestBody User user){
        userServer.updateById(user);
        return R.ok();
    }

    @RequestMapping(value = "/user/delete",method = RequestMethod.DELETE)
    public R delete(@RequestBody Long [] ids){
        userServer.deleteUsers(ids);
        return R.ok();
    }

    @RequestMapping(value = "/user/insert",method = RequestMethod.POST)
    public R insert(@RequestBody List<User> users){
        userServer.insertBath(users);
        return R.ok();
    }

    // 模板导出
    @RequestMapping(value = "/user/exportModel",method = RequestMethod.GET)
    public void exportModel(HttpServletResponse response) throws IOException {
        JxlUtil.downExcelHeader(Arrays.asList("姓名","年龄","地址"),response,"用户信息导入模板.xls");
    }

    // 数据导出
    @RequestMapping(value = "/user/exportExcelHandle", method = RequestMethod.GET)
    public void exportExcelHandle(HttpServletResponse response) throws IllegalAccessException, IOException, WriteException {
        userServer.exportExcelHandle(response);
    }

}
